<template>
  <div class="home page-component">
    <!--    返回顶部-->
    <el-backtop :bottom="100" style="width: 50px;height: 50px">
      <div
        style="font-size: 15px;"
      >
        顶部
      </div>
    </el-backtop>
    <!--    返回顶部结束-->
    <el-carousel indicator-position="outside">
      <!--      <el-carousel-item v-for="item in 2" :key="item">-->
      <el-carousel-item>
        <img src="~assets/images/web-banner1.png" alt="">
      </el-carousel-item>
    </el-carousel>
    <!-- 搜索 -->
    <div class="search-container">
      <div class="search-wrapper">
        <div class="hospital-search">
          <el-autocomplete
            class="search-input"
            prefix-icon="el-icon-search"
            v-model="state"
            :fetch-suggestions="querySearchAsync"
            placeholder="点击输入医院名称"
            @select="handleSelect"
          >
            <span slot="suffix" class="search-btn v-link highlight clickable selected">搜索 </span>
          </el-autocomplete>
        </div>
      </div>
    </div>


    <!-- bottom -->
    <div class="bottom">
      <div class="left">
        <div class="home-filter-wrapper">
          <div class="title"> 医院</div>
          <div>
            <!--            医院等级-->
            <div class="filter-wrapper">
        <span
          class="label">等级：</span>
              <div class="condition-wrapper">
                <span class="item v-link clickable" :class="hostypeActiveIndex == index ? 'selected' : ''"
                      v-for="(item,index) in hostypeList" :key="item.id"
                      @click="hostypeSelect(item.value, index)">{{ item.name }}</span>
              </div>
            </div>
            <!--            省份-->
            <div class="filter-wrapper">
      <span
        class="label">省份：</span>
              <div class="condition-wrapper">
                <span class="item v-link clickable" :class="provinceActiveIndex == index ? 'selected' : ''"
                      v-for="(item,index) in provinceList" :key="item.id"
                      @click="getByParentId(item.value, index)">{{ item.name }}</span>
              </div>
            </div>
            <!--            地区-->
            <div class="filter-wrapper">
      <span
        class="label">地区：</span>
              <div class="condition-wrapper">
                <span class="item v-link clickable" :class="districtActiveIndex == index ? 'selected' : ''"
                      v-for="(item,index) in districtList" :key="item.id"
                      @click="districtSelect(item.value, index)">{{ item.name }}</span>
              </div>
            </div>
          </div>
        </div>
        <div class="v-scroll-list hospital-list">

          <div class="v-card clickable list-item" v-for="item in list" :key="item.id">
            <div class="">
              <div class="hospital-list-item hos-item" index="0" @click="show(item.hoscode)">
                <div class="wrapper">
                  <div class="hospital-title"> {{ item.hosname }}</div>
                  <div class="bottom-container">
                    <div class="icon-wrapper">
                      <span class="iconfont"></span>
                      <span v-if="item.hostype===1">三级甲等</span>
                      <span v-if="item.hostype===2">三级乙等</span>
                      <span v-if="item.hostype===3">二级甲等</span>
                      <span v-if="item.hostype===4">二级乙等</span>
                      <span v-if="item.hostype===5">一级</span>
                      <!--                      {{ item.param.hostypeString }}-->
                    </div>
                    <div class="icon-wrapper">
                      <span class="iconfont"></span>
                      每天{{ item.bookingRule.releasetime }}放号
                    </div>
                  </div>
                </div>
                <img :src="'http://localhost/image/'+item.logodata" :alt="item.hosname" class="hospital-img">
              </div>
            </div>
          </div>
          <p style="margin: 30px 0 auto auto;width: 100%;text-align: center">
            <el-button type="text" @click="load" :disabled="disabled">{{ messageLoad }}</el-button>
          </p>
        </div>
      </div>
      <div class="right">
        <!--        <div class="common-dept">-->
        <!--          <div class="header-wrapper">-->
        <!--            <div class="title"> 常见科室</div>-->
        <!--            <div class="all-wrapper"><span>全部</span>-->
        <!--              <span class="iconfont icon"></span>-->
        <!--            </div>-->
        <!--          </div>-->
        <!--          <div class="content-wrapper">-->
        <!--            <span class="item v-link clickable dark">神经内科 </span>-->
        <!--            <span class="item v-link clickable dark">消化内科 </span>-->
        <!--            <span class="item v-link clickable dark">呼吸内科 </span>-->
        <!--            <span class="item v-link clickable dark">内科 </span>-->
        <!--            <span class="item v-link clickable dark">神经外科 </span>-->
        <!--            <span class="item v-link clickable dark">妇科 </span>-->
        <!--            <span class="item v-link clickable dark"> 产科 </span>-->
        <!--            <span class="item v-link clickable dark">儿科 </span>-->
        <!--          </div>-->
        <!--        </div>-->
        <div class="space">
          <div class="header-wrapper">
            <div class="title-wrapper">
              <div class="icon-wrapper"><span
                class="iconfont title-icon"></span>
              </div>
              <span class="title">平台公告</span>
            </div>
            <div class="all-wrapper">
              <span>全部</span>
              <span class="iconfont icon"></span>
            </div>
          </div>
          <div class="content-wrapper">
            <div class="notice-wrapper" v-for="item in platformNotice" v-bind:key="item.id" @click="platOn(item.id)">
              <div class="point"></div>
              <span class="notice v-link clickable dark">{{ item.title }} </span>
            </div>
            <!--            <div class="notice-wrapper">-->
            <!--              <div class="point"></div>-->
            <!--              <span class="notice v-link clickable dark">关于延长北京大学国际医院放假的通知 </span>-->
            <!--            </div>-->
            <!--            <div class="notice-wrapper">-->
            <!--              <div class="point"></div>-->
            <!--              <span class="notice v-link clickable dark">北京中医药大学东方医院部分科室医生门诊医 </span>-->
            <!--            </div>-->
            <!--            <div class="notice-wrapper">-->
            <!--              <div class="point"></div>-->
            <!--              <span class="notice v-link clickable dark"> 武警总医院号源暂停更新通知 </span>-->
            <!--            </div>-->
          </div>
        </div>
        <div class="suspend-notice-list space">
          <div class="header-wrapper">
            <div class="title-wrapper">
              <div class="icon-wrapper">
                <span class="iconfont title-icon"></span>
              </div>
              <span class="title">停诊公告</span>
            </div>
            <div class="all-wrapper">
              <span>全部</span>
              <span class="iconfont icon"></span>
            </div>
          </div>
          <div class="content-wrapper">
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark"> 中国人民解放军总医院第六医学中心(原海军总医院)呼吸内科门诊停诊公告 </span>
            </div>
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark"> 首都医科大学附属北京潞河医院老年医学科门诊停诊公告 </span>
            </div>
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark">中日友好医院中西医结合心内科门诊停诊公告 </span>
            </div>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>

<script>

import hospApi from '@/api/hosp/hospital'
import dictApi from '@/api/cmn/dict'
import paltApi from "@/api/hosp/palt";

export default {
  //服务端渲染异步，显示医院列表
  // asyncData({ params, error }) {
  //   //调用
  //   // alert("aaa")
  //   return hospApi.getPageList(1,10,null)
  //     .then(response => {
  //       return {
  //         list: response.data.records,
  //         pages: response.data.total
  //       }
  //     })
  // },
  data() {
    return {
      searchObj: {},
      page: 1,  //当前页
      limit: 4,  //每页数据个数

      hosname: '', //医院名称
      hostypeList: [], //医院等级集合
      provinceList: [], //省份集合
      districtList: [], //地区集合
      platformNotice: [],  //公告数据

      hostypeActiveIndex: 0,
      provinceActiveIndex: 0,
      districtActiveIndex: 0,
      list: {},
      pages: 0,

      state: "",

      messageLoad: "点击加载更多",
      disabled: false
    }
  },
  created() {
    hospApi.getPageList(1, 4, null)
      .then(response => {
        console.log("查询医院列表数据", response.data)
        this.list = response.data.records,
          this.pages = response.data.pages

      })

    paltApi.getPaltAll().then(response => {
      console.log("公告数据", response.data)
      this.platformNotice = response.data.records
    })

    this.init()
  },
  methods: {
    //查询医院等级列表 和 所有地区列表
    init() {
      //查询医院等级列表
      dictApi.findByDictCode('dj')
        .then(response => {

          //hostypeList清空
          this.hostypeList = []
          //向hostypeList添加全部值
          this.hostypeList.push({"name": "全部", "value": ""})
          //把接口返回数据，添加到hostypeList
          for (var i = 0; i < response.data.length; i++) {
            this.hostypeList.push(response.data[i])
          }
        })

      //查询地区数据
      dictApi.findByDictCode('city')
        .then(response => {
          this.provinceList = []
          // this.provinceList.push({"name": "全部", "value": ""})
          for (let i in response.data) {
            this.provinceList.push(response.data[i])
          }
        })
      this.getByParentId(110000, 0)
    },
    //根据父id查询
    getByParentId(id, index) {
      this.provinceActiveIndex = index
      // console.log("父级查询数据",id)
      dictApi.findByParentId(id).then(response => {
        this.districtList = []
        this.districtList.push({"name": "全部", "value": ""})
        for (let i in response.data) {
          this.districtList.push(response.data[i])
        }
      })
      // alert("id"+id)
      // console.log("id",id)
      // this.districtSelect(id,0)
    },

    //查询医院列表
    getList() {
      hospApi.getPageList(this.page, this.limit, this.searchObj)
        .then(response => {
          console.log("查询医院列表数据", response.data)
          for (let i in response.data.records) {
            this.list.push(response.data.records[i])
          }
          this.pages = response.data.pages  //总页数
          this.page = response.data.current   //当前页
          // if ()


        })
    },

    //根据医院等级查询
    hostypeSelect(hostype, index) {
      //准备数据
      this.list = []
      this.page = 1
      this.hostypeActiveIndex = index
      this.searchObj.hostype = hostype
      //调用查询医院列表方法
      this.getList()
    },

    //根据地区查询医院
    districtSelect(districtCode, index) {
      // alert("aaa")
      this.list = []
      this.page = 1
      this.districtActiveIndex = index
      this.searchObj.districtCode = districtCode
      this.getList();
    },

    //在输入框输入值，弹出下拉框，显示相关内容
    querySearchAsync(queryString, cb) {
      // console.log("cd---",cb)
      this.searchObj = []
      if (queryString == '') return
      hospApi.getByHosname(queryString).then(response => {
        for (let i = 0, len = response.data.length; i < len; i++) {
          response.data[i].value = response.data[i].hosname
        }
        cb(response.data)
      })
    },

    //在下拉框选择某一个内容，执行下面方法，跳转到详情页面中
    handleSelect(item) {
      window.location.href = '/hospital/' + item.hoscode
    },

    //点击某个医院名称，跳转到详情页面中
    show(hoscode) {
      window.location.href = '/hospital/' + hoscode
    },
    //跳转公告详情页面
    platOn(id) {
      window.location.href = '/plat/' + id
    },
    // 点击继续加载医院
    load() {
      // console.log("查询当前页", this.page, this.pages)
      if (this.page >= this.pages) {
        this.disabled = true
        this.messageLoad = "已经没有更多啦。。。。"
      } else {
        this.page++;
        // console.log("当前页" + this.page)
        this.getList()
      }

    }
  }
}
</script>
